package simple.runtime.components.impl.android;

import simple.runtime.components.消息处理;
import simple.runtime.components.组件容器;
import simple.runtime.components.impl.组件Impl;
import simple.runtime.events.EventDispatcher;

import android.os.Handler;
import android.os.Message;

/**
 * 消息处理组件的实现。
 * 
 * @author 东子 xhwsd@qq.com
 */
public final class 消息处理Impl extends 组件Impl implements 消息处理 {

  private Handler handler;

  /**
   * 创建新的组件。
   *
   * @param container  容纳组件的容器（对于不可见的组件必须是窗口，不可为 {@code null}）
   */
  public 消息处理Impl(组件容器 container) {
    super(container);
    handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {  
        收到消息(msg);
      };
    };
  }

  // 消息处理 实现

  @Override
  public void 收到消息(Object msg) {
    EventDispatcher.dispatchEvent(this, "收到消息", msg);
  }

  @Override
  public Object 创建消息() {
    return Message.obtain();
  }

  @Override
  public Object 生成消息(int what) {
    Message msg = Message.obtain();
    msg.what = what;
    return msg;
  }

  @Override
  public int 取消息标记(Object msg) {
    if(msg instanceof Message) {
      return ((Message) msg).what;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.取消息标记] 消息对象错误");
    }
  }

  @Override
  public void 置消息标记(Object msg, int what) {
    if(msg instanceof Message) {
      ((Message) msg).what = what;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.取信息标记] 消息对象错误");
    }
  }

  @Override
  public int 取消息参数1(Object msg) {
    if(msg instanceof Message) {
      return ((Message) msg).arg1;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.取消息参数1] 消息对象错误");
    }
  }

  @Override
  public void 置消息参数1(Object msg, int value) {
    if(msg instanceof Message) {
      ((Message) msg).arg1 = value;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.置消息参数1] 消息对象错误");
    }
  }

  @Override
  public int 取消息参数2(Object msg) {
    if(msg instanceof Message) {
      return ((Message) msg).arg2;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.取消息参数2] 消息对象错误");
    }
  }

  @Override
  public void 置消息参数2(Object msg, int value) {
    if(msg instanceof Message) {
      ((Message) msg).arg2 = value;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.置消息参数2] 消息对象错误");
    }
  }

  @Override
  public Object 取消息对象(Object msg) {
    if(msg instanceof Message) {
      return ((Message) msg).obj;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.取消息对象] 消息对象错误");
    }
  }

  @Override
  public void 置消息对象(Object msg, Object obj) {
    if(msg instanceof Message) {
      ((Message) msg).obj = obj;
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.置消息对象] 消息对象错误");
    }
  }

  @Override
  public boolean 发送消息(Object msg) {
    if(msg instanceof Message) {
      return handler.sendMessage((Message) msg);
    } else {
      throw new IllegalArgumentException("[" + hashCode()  + "/消息处理.发送消息] 消息对象错误");
    }
  }

}
